using System;
using System.Linq;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Metadata.Entity.DataModel;

namespace T.FAS.Runtime.DataAccess.Common
{
    /// <summary>
    /// DO扩展方法
    /// </summary>
    public static class DOCommonExtension
    {
        /// <summary>
        /// 获取表名
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string GetTableName(this DataObject data)
        {
            if (data == null)
                return string.Empty;
            if (string.IsNullOrEmpty(data.ActualTableNames))
                return data.LogicTableName;
            else
                return data.ActualTableNames;
        }

        /// <summary>
        /// 获取主键列
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static DataColumn GetPkColumn(this DataObject data)
        {
            if (data == null)
                return null;
            return data.Columns?.FirstOrDefault(col => col.IsPkColumn);
        }

        /// <summary>
        /// 是否大字段
        /// </summary>
        /// <param name="dataColumn"></param>
        /// <returns></returns>
        public static bool IsBigField(this DataColumn dataColumn)
        {
            if (dataColumn == null) return false;
            try
            {
                if (!dataColumn.IsFkColumn)
                {
                    var dbColType = DataTypeUtil.ConvertToDatabaseColumnType(dataColumn.DataType);
                    return dbColType == DatabaseColumnType.VarcharMax || dbColType == DatabaseColumnType.NvarcharMax || dbColType == DatabaseColumnType.VarbinaryMax;
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 将要忽略前端输入的系统字段。例如Creator LastModifier
        /// </summary>
        /// <param name="dataColumn"></param>
        /// <returns></returns>
        public static bool IsIgnoreExternalInputSystemField(this DataColumn dataColumn)
        {
            try
            {
                if (dataColumn.IsSystemField())
                {
                    if (!dataColumn.IsTimestampField())
                        return true;
                }
                return false;
            }
            catch (Exception e)
            {
                throw new FasException(DataAccessErrorCode.Metadata.CDO.MetadataError, string.Format(CommonStringClassCommon.DOCommonExtension_ProcDataObjectColumnFail,dataColumn.DataObjectID,dataColumn.ColumnName), e);
            }
        }

        /// <summary>
        /// 是否为系统字段
        /// </summary>
        /// <param name="dataColumn"></param>
        /// <returns></returns>
        public static bool IsSystemField(this DataColumn dataColumn)
        {
            try
            {
                return dataColumn.SysColumnFlag != SysColumnFlag.None || dataColumn.IsSystem;
            }
            catch (Exception e)
            {
                throw new FasException(DataAccessErrorCode.Metadata.CDO.MetadataError, string.Format(CommonStringClassCommon.DOCommonExtension_ProcDataObjectColumnFail, dataColumn.DataObjectID,dataColumn.ColumnName), e);
            }
        }

        /// <summary>
        /// 是否为系统字段
        /// </summary>
        /// <param name="dataColumn"></param>
        /// <returns></returns>
        public static bool IsCreateInfoField(this DataColumn dataColumn)
        {
            try
            {
                return dataColumn.SysColumnFlag == SysColumnFlag.Creator ||
                       dataColumn.SysColumnFlag == SysColumnFlag.CreatorName ||
                       dataColumn.SysColumnFlag == SysColumnFlag.CreateTime ||
                       dataColumn.ColumnName == DataModelSystemConst.CreateTime ||
                       dataColumn.ColumnName == DataModelSystemConst.Creator ||
                       dataColumn.ColumnName == DataModelSystemConst.CreatorName;
            }
            catch (Exception e)
            {
                throw new FasException(DataAccessErrorCode.Metadata.CDO.MetadataError, string.Format(CommonStringClassCommon.DOCommonExtension_ProcDataObjectColumnFail, dataColumn.DataObjectID,dataColumn.ColumnName), e);
            }
        }

        /// <summary>
        /// 是否为时间戳字段
        /// </summary>
        /// <param name="dataColumn"></param>
        /// <returns></returns>
        public static bool IsTimestampField(this DataColumn dataColumn)
        {
            try
            {
                return dataColumn.SysColumnFlag == SysColumnFlag.LastModifyTime || dataColumn.IsTimestamp;
            }
            catch (Exception e)
            {
                throw new FasException(DataAccessErrorCode.Metadata.CDO.MetadataError, string.Format(CommonStringClassCommon.DOCommonExtension_ProcDataObjectColumnFail, dataColumn.DataObjectID,dataColumn.ColumnName), e);
            }
        }

        /// <summary>
        /// 是否为时间戳字段
        /// </summary>
        /// <param name="dataColumn"></param>
        /// <returns></returns>
        public static DatabaseColumnType GetDatabaseColumnType(this DataColumn dataColumn)
        {
            if (DataTypeUtil.TryConvertToDatabaseColumnType(dataColumn.DataType, out DatabaseColumnType databaseColumnType))
            {
                return databaseColumnType;
            }
            else
                return DatabaseColumnType.Varchar;
        }

    }
}
